@extends('home.index')
@section('body')
    <div class="container">
        <div class="loginTit">
            <div class="area">
                <h3>用户注册</h3>
                <span class="desc">以下内容我们承诺您的信息安全，不会透露给第三方友情提示：请您认真填写！</span>
            </div>
        </div>
        <div class="wf100">
            <div class="regcontent">
                <form name=""  id="register">
                    <div class="reg-form">
                        <div class="item">
                            <label class="name">账号</label>
                            <div class="input">
                                <input type="text" name="account" id="account"/>
                            </div>
                            <label class="tips"></label>
                        </div>
                        <div class="item">
                            <label class="name">密码</label>
                            <div class="input">
                                <input type="password" name="pswd" id="pswd"/>
                            </div>
                            <label class="tips"></label>
                        </div>
                        <div class="item">
                            <label class="name">确认密码</label>
                            <div class="input">
                                <input type="password" name="repswd" id="repswd"/>
                            </div>
                            <label class="tips"></label>
                        </div>
                        <div class="item">
                            <label class="name">真实姓名</label>
                            <div class="input">
                                <input type="text" name="name" id="name"/>
                            </div>
                            <label class="tips"></label>
                        </div>
                        <div class="item">
                            <label class="name">身份证号</label>
                            <div class="input">
                                <input type="text" name="idcard" id="idcard"/>
                            </div>
                        </div>
                        {{csrf_field()}}
                        <div class="item" style="text-align: center;">
                            <input type="submit" value="立即注册" id="reg" class="submit"/>
                        </div>
                        <div class="item">
                            <label class="name" style="width:270px;float:left;">&nbsp;</label>
                            <div class="input">
                                <input type="checkbox" name="agree" id="agree" checked="checked" style="margin-left:150px;" /><a href="{{url('yonghuxieyi')}}" style="color:#000">我已经阅读并同意《用户注册服务协议》</a>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection
@section('myjs')

    <script src="{{asset('js/jquery.min.js')}}"></script>
    <script src="{{asset('js/jquery.validate.min.js')}}"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            initVerifyRules();
            //注册操作
            $("#register").validate({
                submitHandler: function (form) {
                    $.ajax({
                        type: "post",
                        url: '/reg',
                        data: $(form).serializeArray(),
                        success: function(result) {
                            console.log(result);
                            if (result == 1) {
                                alert('注册成功,快去登录吧');
                                window.location.href="/login";
                            }
                            if (result == 2) {
                                alert('用户名已存在');
                            }
                            if (result == 3) {
                                alert("注册失败")
                            }
                        }
                    });
                },
                rules: {
                    account: {
                        required: true,
                        chrnum: true,
                    },
                    pswd: "required",
                    repswd: {
                        required: true,
                        equalTo: "#pswd",
                    },
                    name: {
                        required: true,
                        chcharacter: true,
                    },
                    idcard: {
                        required: true,
                        isIdCardNo: true,
                    },
                    agree: "required"
                },
                messages: {
                    account: {
                        required: "请输入注册账号",
                    },
                    pswd: "请输入密码",
                    repswd: {
                        required: "请输入密码",
                        equalTo: "两次密码输入不一致"
                    },
                    name: {
                        required: "您输入的姓名有误",
                    },
                    idcard: {
                        required: "您输入的身份证有误",
                    },
                    agree:"请同意注册服务协议"
                },

            });
        });
        function initVerifyRules() {
            // 身份证号码验证
            jQuery.validator.addMethod("isIdCardNo", function (value, element) {
                return this.optional(element) || nunber(value);
            }, "您输入的身份证有误");
            //数字和字母组合
            jQuery.validator.addMethod("chrnum", function (value, element) {
                var chrnum = /^([a-zA-Z0-9]+)$/;
                return this.optional(element) || (chrnum.test(value));
            }, "账号是数字、字母或组合");

            // 验证姓名
            jQuery.validator.addMethod("chcharacter", function (value, element) {
                var firstName = value.substr(0, 1);
                //为什么汉字占用两个字节，英文字母占用一个字节。截取一个汉字却用一个字节。
                if (!checkbjx(firstName)) {
                    $("#name-error").html("您输入的姓名有误");
                    return false;
                } else {
                    // $("#name-error").html("");
                }
                var username = /^[\u4E00-\u9FA5]{2,6}$/;

                return this.optional(element) || (username.test(value));

            }, "您输入的姓名有误");
        }
        /*//身份证号码的验证规则
         function isIdCardNos(num) {
         var len = num.length, re;
         if (len == 15)
         re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{2})(\w)$/);
         else if (len == 18)
         re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/);
         else {
         return false;
         }
         var a = num.match(re);
         if (a != null) {
         if (len == 15) {
         var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
         var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
         }
         else {
         var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
         var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
         }
         if (!B) {
         return false;
         }
         }
         if (!re.test(num)) {
         return false;
         }
         return true;
         }
         */
        //验证百家姓
        function checkbjx(name) {
            //js正则表达式  match
            //为什么此时的match方法不对
            if ("赵|钱|孙|李|周|吴|郑|王|冯|陈|楮|卫|蒋|沈|韩|杨|朱|秦|尤|许|何|吕|施|张|孔|曹|严|华|金|魏|陶|姜|戚|谢|邹|喻|柏|水|窦|章|云|苏|潘|葛|奚|范|彭|郎|鲁|韦|昌|马|苗|凤|花|方|俞|任|袁|柳|酆|鲍|史|唐|费|廉|岑|薛|雷|贺|倪|汤|滕|殷|罗|毕|郝|邬|安|常|乐|于|时|傅|皮|卞|齐|康|伍|余|元|卜|顾|孟|平|黄|和|穆|萧|尹|姚|邵|湛|汪|祁|毛|禹|狄|米|贝|明|臧|计|伏|成|戴|谈|宋|茅|庞|熊|纪|舒|屈|项|祝|董|梁|杜|阮|蓝|闽|席|季|麻|强|贾|路|娄|危|江|童|颜|郭|梅|盛|林|刁|锺|徐|丘|骆|高|夏|蔡|田|樊|胡|凌|霍|虞|万|支|柯|昝|管|卢|莫|经|房|裘|缪|干|解|应|宗|丁|宣|贲|邓|郁|单|杭|洪|包|诸|左|石|崔|吉|钮|龚|程|嵇|邢|滑|裴|陆|荣|翁|荀|羊|於|惠|甄|麹|家|封|芮|羿|储|靳|汲|邴|糜|松|井|段|富|巫|乌|焦|巴|弓|牧|隗|山|谷|车|侯|宓|蓬|全|郗|班|仰|秋|仲|伊|宫|宁|仇|栾|暴|甘|斜|厉|戎|祖|武|符|刘|景|詹|束|龙|叶|幸|司|韶|郜|黎|蓟|薄|印|宿|白|怀|蒲|邰|从|鄂|索|咸|籍|赖|卓|蔺|屠|蒙|池|乔|阴|郁|胥|能|苍|双|闻|莘|党|翟|谭|贡|劳|逄|姬|申|扶|堵|冉|宰|郦|雍|郤|璩|桑|桂|濮|牛|寿|通|边|扈|燕|冀|郏|浦|尚|农|温|别|庄|晏|柴|瞿|阎|充|慕|连|茹|习|宦|艾|鱼|容|向|古|易|慎|戈|廖|庾|终|暨|居|衡|步|都|耿|满|弘|匡|国|文|寇|广|禄|阙|东|欧|殳|沃|利|蔚|越|夔|隆|师|巩|厍|聂|晁|勾|敖|融|冷|訾|辛|阚|那|简|饶|空|曾|毋|沙|乜|养|鞠|须|丰|巢|关|蒯|相|查|后|荆|红|游|竺|权|逑|盖|益|桓|公|万俟|司马|上官|欧阳|夏侯|诸葛|闻人|东方|赫连|皇甫|尉迟|公羊|澹台|公冶|宗政|濮阳|淳于|单于|太叔|申屠|公孙|仲孙|轩辕|令狐|锺离|宇文|长孙|慕容|鲜于|闾丘|司徒|司空|丌官|司寇|仉|督|子车|颛孙|端木|巫马|公西|漆雕|乐正|壤驷|公良|拓拔|夹谷|宰父|谷梁|晋|楚|阎|法|汝|鄢|涂|钦|段干|百里|东郭|南门|呼延|归|海|羊舌|微生|岳|帅|缑|亢|况|后|有|琴|梁丘|左丘|东门|西门|商|牟|佘|佴|伯|赏|南宫|墨|哈|谯|笪|年|爱|阳|佟|第五|言|福".search(name) != -1
            ) {
                return true;
            }
            else {
                return false;
            }
        }

        //验证新的身份证
        function nunber(allowancePersonValue){
            if(allowancePersonValue==""){
                $("#idcard-error").html("身份证号不能为空");
                return false;
            }
            //校验长度，类型
            else if(isCardNo(allowancePersonValue) === false)
            {
                $("#idcard-error").html("身份证号不能为空");
                return false;
            }
            //检查省份
            else if(checkProvince(allowancePersonValue) === false)
            {
                $("#idcard-error").html("身份证号不能为空");
                return false;
            }
            //校验生日
            else if(checkBirthday(allowancePersonValue) === false)
            {
                $("#idcard-error").html("身份证号不能为空");
                return false;
            }
            //检验位的检测
            else if(checkParity(allowancePersonValue) === false)
            {
                $("#idcard-error").html("身份证号不能为空");
                return false;
            }else{
                return true;
            }
        }

        //身份证省的编码
        var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",
            21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",
            33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",
            42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",
            51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",
            63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"
        };

        //检查号码是否符合规范，包括长度，类型
        function isCardNo(card){
            //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
            var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
            if(reg.test(card) === false){
                //alert("demo");
                return false;
            }
            return true;
        }

        //取身份证前两位,校验省份
        function checkProvince(card){
            var province = card.substr(0,2);
            if(vcity[province] == undefined){
                return false;
            }
            return true;
        }

        //检查生日是否正确
        function checkBirthday(card){
            var len = card.length;
            //身份证15位时，次序为省（3位）市（3位）年（2位）月（2位）日（2位）校验位（3位），皆为数字
            if(len == '15'){
                var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
                var arr_data = card.match(re_fifteen);
                var year = arr_data[2];
                var month = arr_data[3];
                var day = arr_data[4];
                var birthday = new Date('19'+year+'/'+month+'/'+day);
                return verifyBirthday('19'+year,month,day,birthday);
            }
            //身份证18位时，次序为省（3位）市（3位）年（4位）月（2位）日（2位）校验位（4位），校验位末尾可能为X
            if(len == '18'){
                var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
                var arr_data = card.match(re_eighteen);
                var year = arr_data[2];
                var month = arr_data[3];
                var day = arr_data[4];
                var birthday = new Date(year+'/'+month+'/'+day);
                return verifyBirthday(year,month,day,birthday);
            }
            return false;
        }

        //校验日期
        function verifyBirthday(year,month,day,birthday){
            var now = new Date();
            var now_year = now.getFullYear();
            //年月日是否合理
            if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day)
            {
                //判断年份的范围（3岁到100岁之间)
                var time = now_year - year;
                if(time >= 18 && time <= 70)
                {
                    return true;
                }
                return false;
            }
            return false;
        }

        //校验位的检测
        function checkParity(card){
            //15位转18位
            card = changeFivteenToEighteen(card);
            var len = card.length;
            if(len == '18'){
                var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                var cardTemp = 0, i, valnum;
                for(i = 0; i < 17; i ++)
                {
                    cardTemp += card.substr(i, 1) * arrInt[i];
                }
                valnum = arrCh[cardTemp % 11];
                if (valnum == card.substr(17, 1))
                {
                    return true;
                }
                return false;
            }
            return false;
        }

        //15位转18位身份证号
        function changeFivteenToEighteen(card){
            if(card.length == '15')
            {
                var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                var cardTemp = 0, i;
                card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
                for(i = 0; i < 17; i ++)
                {
                    cardTemp += card.substr(i, 1) * arrInt[i];
                }
                card += arrCh[cardTemp % 11];
                return card;
            }
            return card;
        }


    </script>
@endsection